package com.controller;



import com.dto.JsonResult;
import com.entity.Role;
import com.entity.UserInfo;
import com.service.RoleService;
import com.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@RestController
public class DeleteController {

    @Autowired
    private RoleService roleService;

    @Autowired
    private UserService userService;

    @DeleteMapping("/api/admin/user/del")
    public JsonResult delUSerOne(@RequestBody UserInfo user) {
        System.out.println(user);
        System.out.println(user.getId());
        boolean flag = userService.findById(user.getId());
        if (flag) {
            userService.deleteById(user.getId());
            return JsonResult.buildSuccess(200,"删除成功");
        } else {
            return JsonResult.buildFailure(400,"删除失败");
        }
    }

    @DeleteMapping("/api/admin/user/delMore")
    public JsonResult delUserMore(@RequestParam("users") String[] users) {
        List<Object> list = Arrays.asList(Arrays.stream(users).toArray());
        List<Integer> uids = new ArrayList<>();
        System.out.println(list);
        for (Object o : list) {
            uids.add(Integer.valueOf(String.valueOf(o)));
        }
        boolean flag = userService.findByIds(uids);
        if (flag) {
            userService.deleteByIds(uids);
            return JsonResult.buildSuccess(200,"删除成功");
        } else {
            return JsonResult.buildFailure(400,"删除失败");
        }
    }

    @DeleteMapping("/api/admin/role/del")
    public JsonResult delRoleOne(@RequestBody Role role) {
        System.out.println(role);
        System.out.println(role.getId());
        boolean flag = roleService.findById(role.getId());
        if (flag) {
            roleService.deleteRoleById(role.getId());
            return JsonResult.buildSuccess(200,"删除成功");
        } else {
            return JsonResult.buildFailure(400,"删除失败");
        }
    }

    @DeleteMapping("/api/admin/role/delMore")
    public JsonResult delRoleMore(@RequestParam("users") String[] users) {
        List<Object> list = Arrays.asList(Arrays.stream(users).toArray());
        List<Integer> uids = new ArrayList<>();
        System.out.println(list);
        for (Object o : list) {
            uids.add(Integer.valueOf(String.valueOf(o)));
        }
        boolean flag = roleService.findByIds(uids);
        if (flag) {
            roleService.deleteRoleByIds(uids);
            return JsonResult.buildSuccess(200,"删除成功");
        } else {
            return JsonResult.buildFailure(400,"删除失败");
        }
    }

}
